﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Web7.Utilities
{
    public class StringUtils
    {
        /// <summary>
        /// base64解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Base64_Decode(string str)
        {
            byte[] bytes = Convert.FromBase64String(str);
            return Encoding.UTF8.GetString(bytes);
        }

        /// <summary>
        /// base64编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Base64_Encode(string str)
        {
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
        }

        /// <summary>
        /// 过滤xml特殊字符
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string CleanInvalidCharsForXML(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return input;
            }
            StringBuilder builder = new StringBuilder();
            char[] chArray = input.ToCharArray();
            for (int i = 0; i < chArray.Length; i++)
            {
                int num2 = Convert.ToInt32(chArray[i]);
                if ((((num2 < 0) || (num2 > 8)) && ((num2 < 11) || (num2 > 12))) && ((num2 < 14) || (num2 > 0x1f)))
                {
                    builder.Append(chArray[i]);
                }
            }
            return builder.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static string StripSQLInjection(string sql)
        {
            if (!string.IsNullOrEmpty(sql))
            {
                string pattern = @"(\%27)|(\')|(\-\-)";
                string str2 = @"((\%27)|(\'))\s*((\%6F)|o|(\%4F))((\%72)|r|(\%52))";
                string str3 = @"\s+exec(\s|\+)+(s|x)p\w+";
                sql = Regex.Replace(sql, pattern, string.Empty, RegexOptions.IgnoreCase);
                sql = Regex.Replace(sql, str2, string.Empty, RegexOptions.IgnoreCase);
                sql = Regex.Replace(sql, str3, string.Empty, RegexOptions.IgnoreCase);
            }
            return sql;
        }

        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="stringTrim"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static string Trim(string stringTrim, int maxLength)
        {
            return Trim(stringTrim, maxLength, "...");
        }

        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="rawString"></param>
        /// <param name="maxLength"></param>
        /// <param name="appendString"></param>
        /// <returns></returns>
        public static string Trim(string rawString, int maxLength, string appendString)
        {
            if (string.IsNullOrEmpty(rawString) || (rawString.Length <= maxLength))
            {
                return rawString;
            }
            if (Encoding.UTF8.GetBytes(rawString).Length <= (maxLength * 2))
            {
                return rawString;
            }
            int length = Encoding.UTF8.GetBytes(appendString).Length;
            StringBuilder builder = new StringBuilder();
            int num3 = 0;
            for (int i = 0; i < rawString.Length; i++)
            {
                char ch = rawString[i];
                builder.Append(ch);
                num3 += Encoding.Default.GetBytes(new char[] { ch }).Length;
                if (num3 >= ((maxLength * 2) - length))
                {
                    break;
                }
            }
            return (builder.ToString() + appendString);
        }

        /// <summary>
        /// UnicodeEncode编码
        /// </summary>
        /// <param name="rawText"></param>
        /// <returns></returns>
        public static string UnicodeEncode(string rawText)
        {
            if ((rawText == null) || (rawText == string.Empty))
            {
                return rawText;
            }
            string str = "";
            string str4 = rawText;
            for (int i = 0; i < str4.Length; i++)
            {
                int num = str4[i];
                string str2 = "";
                if (num > 0x7e)
                {
                    str = str + @"\u";
                    str2 = num.ToString("x");
                    for (int j = 0; j < (4 - str2.Length); j++)
                    {
                        str = str + "0";
                    }
                }
                else
                {
                    str2 = ((char)num).ToString();
                }
                str = str + str2;
            }
            return str;
        }

    }
}
